var fs = require("fs");
var path = require('path')
var Buffer = require('buffer')


// 删除文件夹
function delFile(path) {
    if (fs.existsSync(path)) {
        if (fs.statSync(path).isDirectory()) {
            let files = fs.readdirSync(path);
            for(let i = 0 ;i<files.length; i++) {
                let file = files[i]
                let currentPath = path + "/" + file;
                if (fs.statSync(currentPath).isDirectory()) {
                    delFile(currentPath);
                } else {
                    fs.unlinkSync(currentPath);
                }
            }
            fs.rmdirSync(path);
        } else {
            fs.unlinkSync(path);
        }
    }
}



const StreamZip = require('node-stream-zip')
const zip = new StreamZip({
    file: path.join(__dirname,  './download.zip'),
    storeEntries: true
})

zip.on('ready', () => {
    fs.mkdirSync(path.join(__dirname, './download'))

    zip.extract(null, path.join(__dirname, './download') , (err, count) => {
        console.log(err, count);
        const dirStr = fs.readdirSync(path.join(__dirname, './download'))

        const iconBuffer = fs.readFileSync(path.join(__dirname, `./download/${dirStr[0]}/iconfont.css`))
        var iconStr = iconBuffer.toString()
        iconStr = iconStr.slice(iconStr.indexOf('.icon-'))

        iconStr = `
@font-face {
    font-family: "iconfont";
    src: url('iconfont.woff2') format('woff2');
}
${iconStr}
        `
        fs.unlink(path.join(__dirname, '../../src/styles/fonts.less'), error => {
            if(!error){ }
            fs.writeFile( path.join(__dirname, '../../src/styles/fonts.less'), iconStr, error => {
                if(error) {
                    console.log(error);
                    return false
                }
                fs.writeFileSync( path.join(__dirname, '../../src/styles/iconfont.woff2') , fs.readFileSync(path.join(__dirname, `./download/${dirStr[0]}/iconfont.woff2`)));
                delFile(path.join(__dirname, './download'))
                delFile(path.join(__dirname, './download.zip'))
                console.log( 'fonts.less写入成功')
            })
        })







    })
})
